package com.tdcoding.chat.dbase;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.tdcoding.chat.entities.ComplainInfo;
import com.tdcoding.chat.helper.Logger;
import com.tdcoding.chat.manager.DBManager;

/**
 * 地区管理
 * 
 * @author Wellsa
 *
 */
public class DBForComplain {
	private static final String TAG = "DBForComplain";
	private static final String COMPLAIN_TABLE = "ofComplain";
	
	/**
	 * 添加群举报
	 * @return
	 */
	static final String COMPLAIN_COLUMNS = "userId,complainId,complainType,complainInfo,creationDate";
	@SuppressWarnings("unchecked")
	public static int addComplainInfo(long userId,Object[] objs) {
		long complainId =Long.parseLong(objs[0].toString());
		int complainType=Integer.parseInt(objs[1].toString());
		List<String> complains =(List<String>) objs[2];
		String values = "userId="+userId+",complainId="+complainId+",complainType="+complainType
				+ ",complainInfo='" + complains.toString()+"',creationDate='"+System.currentTimeMillis();
		int result = DBManager.insertSql(0, COMPLAIN_TABLE,COMPLAIN_COLUMNS,
				values);
		return result ;
	}
	
	/**
	 * 查询群舉報信息
	 * @param complainId
	 * @param groupId
	 * @return
	 */
	public static List<ComplainInfo> queryComplainById(long userId,long complainId) {
		List<ComplainInfo> complainInfos=new ArrayList<ComplainInfo>();
		String where = "userId="+userId+" and complainId=" + complainId;
		String sql = "select * from "+COMPLAIN_TABLE+" where " + where;
		try {
			ResultSet resultSet = DBManager.querySql(0,
					sql);
			ComplainInfo complainInfo =new ComplainInfo();
			complainInfo.setComplainId(complainId);
			while (resultSet.next()) {
				complainInfo.setUserId(resultSet.getLong("userId"));
				complainInfo.setComplainInfo(resultSet.getString("complainInfo"));
				complainInfo.setCreationDate(resultSet.getString("creationDate"));
				complainInfos.add(complainInfo);
			}
			Logger.println(TAG, "queryComplainById", complainInfos.size());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return complainInfos;
	}
	
	/**
	 * 查询群舉報信息
	 * @param complainId
	 * @param groupId
	 * @return
	 */
	public static List<ComplainInfo> queryComplainByType(long userId,int complainType) {
		List<ComplainInfo> complainInfos=new ArrayList<ComplainInfo>();
		String where = "userId="+userId+" and complainType="+complainType;
		String sql = "select * from "+COMPLAIN_TABLE+" where " + where;
		try {
			ResultSet resultSet = DBManager.querySql(0,
					sql);
			while (resultSet.next()) {
				ComplainInfo complainInfo = new ComplainInfo();
				complainInfo.setUserId(resultSet.getLong("userId"));
				complainInfo.setComplainId(resultSet.getLong("complainId"));
				complainInfo.setComplainInfo(resultSet.getString("complainInfo"));
				complainInfo.setCreationDate(resultSet.getString("creationDate"));
				complainInfos.add(complainInfo);
			}
			Logger.println(TAG, "queryComplainByType", complainInfos.size());
		} catch (SQLException e) {	}
		return complainInfos;
	}
	
	/**
	 * 删除群舉報
	 * @param complainId
	 * @return
	 */
	public static int deleteMyComplain(long userId){
		String where="userId="+userId;
		int result = DBManager.deleteSql(0,COMPLAIN_TABLE, where);
		return result;
	}
	
	/**
	 * 删除群举报
	 * @param complainId
	 * @return
	 */
	public static int deleteComplainById(long userId,long complainId){
		String where = "userId="+userId+" and complainId=" + complainId;
		int result = DBManager.deleteSql(0,COMPLAIN_TABLE, where);
		return result;
	}
	
	/**
	 * 删除群举报
	 * @param complainType
	 * @return
	 */
	public static int deleteComplainByType(long userId,int complainType){
		String where = "userId="+userId+" and complainType="+complainType;
		int result = DBManager.deleteSql(0,COMPLAIN_TABLE, where);
		return result;
	}
}
